﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ModbusTestTool.Singeltons;

namespace ModbusTestTool.UI
{
    public partial class MainWindowV2 : Form
    {
        public MainWindowV2()
        {
            InitializeComponent();
            List<string> states = SettingsSingelton.Instance.getListState();
            for (int i = 0; i < states.Count; i++)
            {
                ToolStripMenuItem item = new ToolStripMenuItem();

                item.Text = states[i];
                item.Name = states[i];
                item.DropDownItems.AddRange(new ToolStripItem[] { new ToolStripMenuItem() { Text = "Open" }, new ToolStripMenuItem() { Text = "Close" } });
                item.DropDownItems[0].Click += new EventHandler(MainWindowV2_Click_open);
                item.DropDownItems[1].Click += new EventHandler(MainWindowV2_Click_close);
                settingsToolStripMenuItem.DropDownItems.Add(item);
            }
            
        }

        void MainWindowV2_Click_open(object sender, EventArgs e)
        {
           string tag = (((ToolStripMenuItem)sender).OwnerItem.Name);
           SettingsSingelton.Instance.getState(tag).openPort();
            
            //throw new NotImplementedException();
        }

        void MainWindowV2_Click_close(object sender, EventArgs e)
        {
            string tag = (((ToolStripMenuItem)sender).OwnerItem.Name);
            SettingsSingelton.Instance.getState(tag).closePort();
            //throw new NotImplementedException();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            Application.Exit();
        }

        FileSelectControl FileSelectPanel = new FileSelectControl();
        ErrorPanel errorPanel = new ErrorPanel();
        SettingsPaneV2 Settingspanel = new SettingsPaneV2();
        SettingsPaneV3 Settingspanel2 = new SettingsPaneV3();

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {



            if (e.Node.Name == "FileSelect")
            {
                RemoveExistingPanel();

                
                setPanelAnchor(FileSelectPanel);
                splitContainer1.Panel2.Controls.Add(FileSelectPanel);
            }
            else if (e.Node.Name == "Error")
            {
                RemoveExistingPanel();

                
                setPanelAnchor(errorPanel);
                splitContainer1.Panel2.Controls.Add(errorPanel);
                
            }
            else if (e.Node.Name == "Port")
            {
                RemoveExistingPanel();

                
                setPanelAnchor(Settingspanel);
                splitContainer1.Panel2.Controls.Add(Settingspanel);

            }
            else if (e.Node.Name == "SettingsV2")
            {
                RemoveExistingPanel();
                setPanelAnchor(Settingspanel2);
                splitContainer1.Panel2.Controls.Add(Settingspanel2);
            }
            else
            {

                RemoveExistingPanel();
            }
            
        }

        void RemoveExistingPanel()
        {
            if (splitContainer1.Panel2.Controls.Count != 0)
            {
                splitContainer1.Panel2.Controls.RemoveAt(0);
            }
        }

        void setPanelAnchor(Control panel)
        {
            panel.Anchor = (AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom);
            panel.Dock = DockStyle.Fill;
        }

        /// <summary>
        /// When this menu is clicked lets color code the control
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {



            //settingsToolStripMenuItem.DropDownItems.Add();
            
            //ColorConnect();
            //this is top list with all tags
            foreach (ToolStripMenuItem item in settingsToolStripMenuItem.DropDownItems)
            {
                ColorConnect(item);
                
            }
            
        }

        /// <summary>
        /// This function collers the item to see if they are open or not
        /// </summary>
        /// <param name="item"></param>
        private void ColorConnect(ToolStripMenuItem item)
        {
            if (SettingsSingelton.Instance.getState(item.Text).isOpen())
            {
                item.DropDownItems[0].BackColor = Color.Green;
                item.DropDownItems[1].BackColor = SystemColors.Control;
            }
            else
            {
                item.DropDownItems[0].BackColor = SystemColors.Control;
                item.DropDownItems[1].BackColor = Color.Red; ;
            }

            //if (!SettingsSingelton.Instance.getState(item.Text).isOpen())
            //{
            //    item.DropDownItems[1].BackColor = Color.Red;
            //}
            //else
            //{
            //    item.DropDownItems[0].BackColor = SystemColors.Control;
            //}
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Settings.Instance.closePort();
            //ColorConnect();
        }

        private void openToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Settings.Instance.openPort();
            //ColorConnect();
        }

        private void portToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}
